//
// Created by Semir on 2020/5/12.
//

#include <iostream>

using namespace std;

struct ListNode {
    int val;
    struct ListNode *next;
    explicit ListNode(int x) : val(x), next(nullptr) {}
};

ListNode* linkList(int *arr, int length) {
    auto* pHead = new ListNode(arr[0]);
    auto* cNode = pHead;
    for (int i = 1; i < length; i++) {
        cNode->next = new ListNode(arr[i]);
        cNode = cNode->next;
    }
    return pHead;
}

ListNode* ReverseList(ListNode* pHead) {
    if (pHead == nullptr) {
        return nullptr;
    }
    if (pHead->next != nullptr) {
        ListNode* nNode = pHead->next;
        ListNode* nHead = ReverseList(nNode);
        nNode->next = pHead;
        pHead->next = nullptr;
        return nHead;
    } else {
        return pHead;
    }
}

int main() {
    int arr[5] = {1,2,3,4,56};
    ListNode* pHead = linkList(arr, 5);
    ListNode* nHead = ReverseList(pHead);
    cout << nHead << endl;
    return 0;
}